require 'date';

class TaskController < ApplicationController
  
  # add
  def add
    if params[:project_id]
        @project = get_project(params[:project_id]);
    else 
        @iteration = get_iteration(params[:iteration_id]);
        @project = @iteration.project;
    end	

    @active_users = User.active_users;
    @statuses = StatusTask.find(:all);

    return unless params[:form_submitted];
    
    task_data = Hash.new;
    task_data = params[:task];
    
    task_data[:time_worked]    = 0 if params[:task][:time_worked] == '';
    task_data[:time_worked]   *= 60 if params[:time_worked_unit] == 'hours';
    task_data[:estimated_time] = 0 if params[:task][:estimated_time] == '';
    task_data[:estimated_time]*= 60 if params[:estimated_time_unit] == 'hours';
    task_data[:created]        = DateTime.now;
    task_data[:requestor]      = session[:user].email; 
    task_data[:project_fk]     = @project.project_id;
    task_data[:iteration_fk]   = params[:project_id] ? nil : @iteration.iteration_id;

    @task = Task.new(task_data);
    return unless @task.save();

    flash[:info_msg] = "Task added";
    redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
  end

  # quick create
  def quick_create
      if params[:project_fk] == nil
          redirect_to(:controller => 'root', :action => 'home');
	  return;
      end
      redirect_to(:controller => 'task', :action => 'add', :id => params[:project_fk]);
  end

  def edit
     get_meta_comment;
     @project = @task.project;

    return unless params[:form_submitted];
    
    return unless @task.update_attributes(params[:task]);

    flash[:info_msg] = "Task modified";
    redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
  end

  def list
    @obj = params[:iteration_id] ? get_iteration(params[:iteration_id]) : get_project(params[:project_id]);

    @tasks = @obj.tasks;
  end

  def show
     # show task and comments
     @task = get_task(params[:id]);
     @comments = @task.comments;
  end


  # history
  def comment_history
    @task = get_task(params[:id]);
    @comments = @task.comments;
  end

  # add comment
  def add_comment
     get_meta_comment;
     return unless params[:form_submitted];

     comment_task_data = Hash.new;
     comment_task_data = params[:comment_task];
     comment_task_data[:user_fk] = session[:user].user_id;
     comment_task_data[:added] = DateTime.now;
     if @task.status_fk != params[:task][:status_fk]
         new_status = StatusTask.find(:first, 
	                              :conditions => ['status_task_id = ?', params[:task][:status_fk]]);
         comment_task_data[:subject] = 'status changed from ' + @task.status.name + ' to ' + new_status.name ;
     end
     comment_task_data[:time_worked] = 0 if params[:comment_task][:time_worked] == '';

     @comment_task = CommentTask.new(comment_task_data);
     return unless @comment_task.save();
     # update task
     return unless @task.update_attributes(:owner       => params[:task][:owner], 
                                           :status_fk   => params[:task][:status_fk], 
                                           :time_worked => @task.time_worked + @comment_task.time_worked);

     flash[:info_msg] = "Comment added";
     redirect_to(:action => 'show', :id => @task.task_id);
  end

  # take
  def take
      get_task;
      if (@task.owner != nil) 
          flash[:error_msg] = 'Task already owned';
	  redirect_to(:controller => 'root', :action => 'home');
	  return;
      end
      if (@task.owner == session[:user].user_id) 
          flash[:error_msg] = 'Task already owned by you';
	  redirect_to(:controller => 'root', :action => 'home');
	  return;
      end

     old_owner = @task.owner ? @task.user.username : 'nobody';
     comment_task_data = Hash.new;
     comment_task_data = {
         :user_fk      => session[:user].user_id ,
	 :task_fk      => @task.task_id ,
         :added        => DateTime.now ,
         :content      => 'task taken',
         :subject      => 'owner changed from ' + old_owner + ' to ' + session[:user].username ,
         :time_worked  => 0 
     };
     @comment_task = CommentTask.new(comment_task_data);
     unless @comment_task.save()
         flash[:error_msg] = 'Could not assign this task to you';
         redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
	 return;
     end


      if @task.update_attributes(:owner => session[:user].user_id, :status_fk => @task.status_fk.to_i == 1 ? 2 : @task.status_fk)
          flash[:info_msg] = 'Task is owned by you now';
	  redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
	  return;
      end

      flash[:error_msg] = 'Server error. Action not completed';
      redirect_to(:controller => 'root', :action => 'home');
  end

  # steal
  def steal
      get_task;
      if (@task.owner == session[:user].user_id) 
          flash[:error_msg] = 'Task already owned by you';
	  redirect_to(:controller => 'root', :action => 'home');
	  return;
      end

      old_owner = @task.owner ? @task.user.username : 'nobody';
      comment_task_data = Hash.new;
      comment_task_data = {
         :user_fk      => session[:user].user_id ,
	 :task_fk      => @task.task_id ,
         :added        => DateTime.now ,
         :content      => 'task stealed',
         :subject      => 'owner changed from ' + old_owner + ' to ' + session[:user].username ,
         :time_worked  => 0 
      };
      @comment_task = CommentTask.new(comment_task_data);
      unless @comment_task.save()
         flash[:error_msg] = 'Could not assign this task to you';
         redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
	 return;
      end

      if @task.update_attributes(:owner => session[:user].user_id)
          flash[:info_msg] = 'Task is owned by you now';
	  redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
	  return;
      end

      flash[:error_msg] = 'Server error. Action not completed';
      redirect_to(:controller => 'root', :action => 'home');
  end


  # close
  def close
      get_meta_comment;
      if (@task.status.name == 'closed') 
          flash[:error_msg] = 'Task already closed';
	  redirect_to(:controller => 'root', :action => 'home');
	  return;
      end
      return unless params[:form_submitted];
     
      comment_task_data = Hash.new;
      comment_task_data = params[:comment_task];
      comment_task_data[:user_fk] = session[:user].user_id;
      comment_task_data[:added] = DateTime.now;
      
      old_status = @task.status.name;
      if @task.status_fk != params[:task][:status_fk]
         comment_task_data[:subject] = 'status changed from ' + old_status + ' to closed';
      end
      comment_task_data[:time_worked] = 0 if params[:comment_task][:time_worked] == '';

      @comment_task = CommentTask.new(comment_task_data);
      return unless @comment_task.save();

      old_time_worked = @task.time_worked.to_i;

      if @task.update_attributes(:status_fk => 3, :time_worked => old_time_worked + params[:comment_task][:time_worked].to_i);
          flash[:info_msg] = 'Task closed';
	  redirect_to(:controller => 'task', :action => 'show', :id => @task.task_id);
	  return;
      end

      flash[:error_msg] = 'Server error. Action not completed';
      redirect_to(:controller => 'root', :action => 'home');
  end


  protected

  def get_meta_comment
     @task = get_task(params[:id]);
     @active_users = User.active_users;
     @statuses = StatusTask.find(:all);
  end

end
